package org.ala.shopping_test.shopping_center.repository.order.router;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Map;

import org.ala.shopping_test.shopping_center.repository.shard_key.ShoppingOrderShardKey;
import org.ala.shopping_test.shopping_center.repository.shard_key.ShoppingOrderShardKeyRouter;
import org.ala.tiktools.sharddb.mybatis.router.IShardRouter;
import org.ala.shopping_test.shopping_center.repository.order.dao.ShoppingOrderMapper.MultipleUpdate;
import org.ala.tiktools.sharddb.mybatis.router.ShardInfo;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.IShardKeyConverter;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.IShardKeyRouter;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.ShardKeyRouteWorker;
import org.ala.tiktools.tools.TimestampTools;

import java.util.List;
import java.util.stream.Collectors;

/**
 *	分库分表路由器 (shopping_order)
 *  订单总表
 *
 *	@author 骆毅 (ala)
 *	@date 2025-03-19 15:17:51
 */
public class ShoppingOrderMultipleUpdateListRouter implements IShardRouter<List<MultipleUpdate>> {
    /**
     *  分片建路由器
     */
    protected IShardKeyRouter<ShoppingOrderShardKey> router = new ShoppingOrderShardKeyRouter();
    /**
     *  分片建转换器
     */
    protected IShardKeyConverter<MultipleUpdate, ShoppingOrderShardKey> converter = new IShardKeyConverter<MultipleUpdate, ShoppingOrderShardKey>() {
        @Override
        public ShoppingOrderShardKey convert(MultipleUpdate d) {
            ShoppingOrderShardKey k = ShoppingOrderShardKey.build()
                    .id(d.getQuery().getId())
                    .idIn(d.getQuery().getId())
                    .db(d.getQuery().getShardDB())
                    .dbs(d.getQuery().getShardDBIn())
                    .timestamp(d.getQuery().getCreateTime() != null ? d.getQuery().getCreateTime() : -1)
                    .timestampGte(d.getQuery().getCreateTimeGte() != null ? d.getQuery().getCreateTimeGte() : (d.getQuery().getCreateTimeGt() != null ? d.getQuery().getCreateTimeGt() + 1 : -1))
                    .timestampLte(d.getQuery().getCreateTimeLte() != null ? d.getQuery().getCreateTimeLte() : (d.getQuery().getCreateTimeLt() != null ? d.getQuery().getCreateTimeLt() - 1 : -1))
                    ;
            return k;
        }

        @Override
        public MultipleUpdate deConvert(MultipleUpdate d, ShoppingOrderShardKey k) {
            MultipleUpdate _d = d.copy();
            _d.getQuery()
                    .id(k.getId())
                    .idIn(k.getIdIn())
                    .createTime(k.getTimestamp() > 0 ? k.getTimestamp() : null)
                    .createTimeGte(k.getTimestampGte() > 0 ? k.getTimestampGte() : null)
                    .createTimeLte(k.getTimestampLte() > 0 ? k.getTimestampLte() : null)
                    .shardDB(k.getDb())
                    .shardDBIn(k.getDbs())
                    ;
            return _d;
        }
    };


    /**
     * 分库分表
     */
    @Override
    public Map<ShardInfo, List<MultipleUpdate>> route(List<MultipleUpdate> d) {
        return ShardKeyRouteWorker.instance().route(d, router, converter);
    }
}